package server

import (
	"account/drivers"
	"account/models"
)

// 获取停用的市场信息
func GetMarketBlackAllInfos() ([]models.Market, error) {
	var markets []models.Market

	if result := drivers.SqlClusterDb.
		Where(map[string]interface{}{"deleted": 1, "active": 0}).
		Or(map[string]interface{}{"deleted": 1, "active": 1}).
		Or(map[string]interface{}{"deleted": 0, "active": 0}).
		Or(map[string]interface{}{"deleted": "", "active": ""}).
		Find(&markets).
		GetErrors(); len(result) > 0 {
		return markets, result[0]
	}
	return markets, nil
}

// 获取所有的市场信息
func GetMarketAllInfos() ([]models.Market, error) {
	var markets []models.Market

	if result := drivers.SqlClusterDb.Where(map[string]interface{}{"deleted": 0, "active": 1}).Find(&markets).GetErrors(); len(result) > 0 {
		return markets, result[0]
	}
	return markets, nil
}

// 增加
func AddMarketInfo(market models.Market) error {
	if err := drivers.SqlClusterDb.Create(&market).Error; err != nil {
		return err
	}
	return nil
}

// 更新
func UpdateMarketInfo(id int, market models.Market) error {
	if err := drivers.SqlClusterDb.Model(&models.Market{}).Where("id = ?", id).Update(
		map[string]interface{}{"name": market.Name, "code": market.Code, "active": market.Active, "deleted": market.Deleted}).Error; err != nil {
		return err
	}
	return nil
}

// 软删除
func ActiveMarketInfo(id int) error {
	if err := drivers.SqlClusterDb.Model(&models.Market{}).Where("id = ?", id).Update("active", 0).Error; err != nil {
		return err
	}
	return nil
}
